package com.fund.product.controller;

import com.fund.common.pojo.PageResult;
import com.fund.common.pojo.Result;
import com.fund.product.pojo.dto.ProductPageDTO;
import com.fund.product.pojo.entity.Product;
import com.fund.product.pojo.vo.ProductVO;
import com.fund.common.retention.ParamParse;
import com.fund.product.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

@RestController
@RequestMapping("/product")
@CrossOrigin
public class ProductController {

    @Autowired
    ProductService productService;

    @PostMapping
    public Result<Boolean> saveOrUpdateProduct(@RequestBody Product product) {
        return productService.saveOrUpdateProduct(product);
    }

    @DeleteMapping("/{id}")
    public Result<Boolean> deleteProduct(@PathVariable Integer id) {
        return productService.deleteProductById(id);
    }

    @GetMapping("/page")
    public Result<PageResult<ProductVO>> pageQuery(@ParamParse ProductPageDTO productDTO) {
        return productService.pageQuery(productDTO);
    }

    @GetMapping("/productCode")
    public Result<List<ProductVO>> fuzzySearch(@RequestParam String fundQuery) {
        return productService.findByCode(fundQuery);
    }

    @GetMapping
    public Result<ProductVO> getById(@RequestParam Long id) {
        return productService.getProductById(id);
    }

    /**
     *更新所有产品的净值
     * @return
     */
    @PostMapping("/worth")
    public Result<Boolean> updateWorth(){
        return productService.updateAllWorth();
    }

    /**
     * 根据id列表查询最新净值，返回 id->净值 的map
     * @return
     */
    @GetMapping("/worth")
    public Result<HashMap<Long, BigDecimal>> getNetWorthByIds(List<Long> idList){
        return productService.getNetWorthByIds(idList);
    }

    @GetMapping("/test")
    public String test() {
        System.out.println("收到test请求");
        return "test";
    }


}
